package com.tywj.panda.customer.common.manager

import com.tywj.panda.customer.common.cache.CommonCache

/**
 * <pre>
 *     author : June Yang
 *     time   : 2020/6/9
 *     desc   :验证管理
 *     version: 1.0
 * </pre>
 */
object AuthorityManager {
    // 设备唯一识别码（退出登录或刷新token失败会重置）
    var uid = ""
        private set

    var userType = 1
        private set

    // 用户识别码token
    var token = ""
        private set

    //融云token
    var rongyunToken = ""
        private set

    // 更新token结果回调
    private val updateTokenCallback = ArrayList<() -> Unit>()

    init {
        token = CommonCache.token
        rongyunToken = CommonCache.rongYunToken
        uid = CommonCache.uid
    }

    fun updateToken(token: String) {
        //服务器要求Token加空格
        this.token = "Token $token"
        CommonCache.token = "Token $token"
        updateTokenCallback.forEach { it.invoke() }
        updateTokenCallback.clear()
    }

    //方法参数加?,接受null空参数
    fun updateRongYunToken(rongYunToken: String?) {
        val token = rongYunToken ?: ""
        this.rongyunToken = token
        CommonCache.rongYunToken = token
    }

    fun saveUid(uid: String) {
        this.uid = uid
        CommonCache.uid = uid
    }

    fun saveLoginType(userType: Int) {
        this.userType = userType
        CommonCache.userType = userType
    }

    fun clear() {
        token = ""
        CommonCache.uid = ""
        CommonCache.token = ""
        CommonCache.rongYunToken = ""
        updateTokenCallback.clear()
    }

    fun addUpdateTokenCallback(callback: () -> Unit) {
        updateTokenCallback.add(callback)
    }
}